package cn.lanqiao.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.lanqiao.entity.Pages;
import cn.lanqiao.entity.Paper_manage;

@CrossOrigin
@RequestMapping("/api/paper")
@RestController
public class Paper_manageController {

	@Autowired
	private cn.lanqiao.service.Paper_manageService paper_manageService;
	
	//分页
	@GetMapping(value="/paperpage")
	public Pages<Paper_manage> pager(
			@RequestParam(value="pageindex") int pageindex,
			@RequestParam(value="pagesize") int pagesize,
			@RequestParam(value="searchkey",required=false,defaultValue = "") String searchkey){
		return paper_manageService.PaperPage(pageindex, pagesize, searchkey);
	}
	
	//获取详细信息
	@ GetMapping("/get/{paperId}")
	public Map<String,Object> get(@PathVariable (value="paperId") Integer paperId){
		Map<String,Object>  map = new HashMap<String,Object>();
		map = paper_manageService.getPaperById(paperId);
		if(map.size()>0) {
			map.put("status", 200);
			map.put("statusText","获取数据成功 ");
		}else {
			map.put("status", 0);
			map.put("statusText","获取数据失败 ");
		}
		return map;
	}
	
	//更新
//	@PutMapping("/update")
//	public Map<String , Object > update( @RequestBody  Student student ){
//		int result = studentService.updateStudent(student);
//		Map<String,Object>  map = new HashMap<String,Object>();
//		if( result >0 ) {
//			map.put("status", 200);
//			map.put("statusText", "更新成功");
//		} else {
//			map.put("status", 0);
//			map.put("statusText", "更新失败");
//		}
//		return map;
//	}
	
	//删除
	@DeleteMapping("/delete/{paperId}")
	public Map<String,Object> delete(@PathVariable (value="paperId") Integer paperId){
		Map<String,Object>  map = new HashMap<String,Object>();
		map = paper_manageService.deletePaper(paperId);
		if(map.size()>0) {
			map.put("status", 200);
			map.put("statusText", "删除成功");
		}else{
			map.put("status", 0);
			map.put("statusText", "删除失败");
		}
		return map;
	}
		
	//添加
	@PostMapping("/insert")
	public Map<String,Object> insert(@RequestBody Paper_manage paper){
		Map<String,Object>  map = new HashMap<String,Object>();
		int result = paper_manageService.insertPaper(paper);
		if(result>0) {
			Paper_manage paperDetailed = paper_manageService.insertDetailed(paper);
			Integer paperId = paperDetailed.getPaperId();
			map.put("paperId", paperId);
			map.put("status", 200);
			map.put("statusText", "添加成功");
		}else{
			map.put("paperId", 0);
			map.put("status", 0);
			map.put("statusText", "添加失败");
		}
		return map;
	}		
		
	@GetMapping("/papersubject")
	public List<Paper_manage> Students ( @RequestParam(value="subject",required=false,defaultValue = "") String subject ){
		return paper_manageService.getAllBySubject(subject);
	}
	
	
}
